var PersonCapacityInfo = function (capacityList, month) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (capacityList, month) {
      var month = moment(month).valueOf();
      self.month = month;

      this.setDateList();
      this.setCapacityList(capacityList);
      self.rowIndex = 0;
      self.colIndex = 0;
      self.searchCapacityList = [];
    },

    setDateList: function () {
      var month = self.month;
      var dayMoment = moment(month).startOf('month');
      var endMoment;
      if (moment().isSame(month, 'month')) {
        endMoment = moment();
      } else {
        endMoment = moment(month).endOf('month');
      }

      var dateList = [];
      while (dayMoment.isSameOrBefore(endMoment, 'day')) {
        var day = moment(dayMoment).format('YYYY-MM-DD');
        dateList.push(day);
        dayMoment = dayMoment.add(1, 'day');
      }
      dateList.reverse();
      self.dateList = dateList;
    },

    setCapacityList: function (capacityList) {
      for (var i = 0; i < capacityList.length; i++) {
        var capacity = capacityList[i];
        capacity.id = SysUtil.getId();
        this.formatData(capacity);
      }
      self.capacityList = capacityList;
    },

    formatData: function (capacity) {
      var sumCapacity = 0,
        sumPrice = 0;
      var detailList = capacity.detailList;
      var detailObj = {};
      for (var i = 0; i < detailList.length; i++) {
        var detail = detailList[i];
        sumCapacity += detail.capacity;
        sumPrice += detail.sumPrice;
        detailObj[detail.time] = detail;
      }
      capacity.sumCapacity = _.round(sumCapacity, 0);
      capacity.sumPrice = _.round(sumPrice, 2);

      var dateList = self.dateList;
      for (var i = 0; i < dateList.length; i++) {
        var date = dateList[i];
        var detail = detailObj[date];
        if (detail === undefined) {
          detailObj[date] = {
            time: date,
            sumPrice: 0,
            capacity: 0
          };
        }
      }
      capacity.detailObj = detailObj;
    }
  };

  this.resetCapacityList = function (deptWork, searchType) {
    var capacityList = this.capacityList;
    if (searchType === 1) {
      capacityList = _.filter(capacityList, {
        deptId: deptWork[0]
      });
    } else if (searchType === 2) {
      capacityList = _.filter(capacityList, {
        deptId: deptWork[0],
        workShopId: deptWork[1]
      });
    }
    this.capacityList = capacityList;
  }

  this.getDropdownData = function (searchType) {
    var capacityList = this.capacityList;
    var fields = ['processId', 'processName', 'userId', 'userName'];
    var uniqFields = ['processId', 'userId'];
    if (searchType === 0) {
      fields.push('deptId', 'deptName', 'workshopId', 'workshopName');
      uniqFields.push('deptId', 'workshopId');
    } else if (searchType === 1) {
      fields.push('workshopId', 'workshopName');
      uniqFields.push('workshopId');
    }

    var dropdownData = _.map(capacityList, function (capacity) {
      var obj = {};
      for (var i = 0; i < fields.length; i++) {
        var field = fields[i];
        var valueField = field;
        if (valueField === 'workshopId') {
          valueField = 'workShopId';
        } else if (valueField === 'workshopName') {
          valueField = 'workShopName';
        }
        obj[field] = capacity[valueField];
      }
      return obj;
    });
    return dropdownData;
  };

  this.toSearchData = function (search) {
    var capacityList = this.capacityList;
    if (capacityList.length === 0) {
      this.searchCapacityList = [];
      return;
    }

    var searchObj = search.getSearchObj();
    if (searchObj !== null) {
      capacityList = _.filter(capacityList, searchObj);
    }

    var searchCapacityList = [];
    for (var i = 0; i < capacityList.length; i++) {
      var capacity = capacityList[i];
      capacity = _.cloneDeep(capacity);
      capacity.rowIndex = i;
      searchCapacityList.push(capacity);
    }
    this.searchCapacityList = searchCapacityList;
  };

  this.getCapacityList = function () {
    var rowIndex = this.rowIndex;
    var capacitys = this.searchCapacityList;
    if (rowIndex === 0) {
      return _.clone(capacitys);
    }

    var capacityList = capacitys.slice(rowIndex);
    var capacityArray = capacitys.slice(0, rowIndex);
    capacityList.pushArray(capacityArray);
    return capacityList;
  };

  this.getDateList = function(){
    var colIndex = this.colIndex;
    var dates = this.dateList;
    if (colIndex === 0) {
      return dates;
    }

    var dateList = dates.slice(colIndex);
    var dateArray = dates.slice(0, colIndex);
    dateList.pushArray(dateArray);
    return dateList;
  };

  this.incrementRowIndex = function(){
    var rowIndex = this.rowIndex;
    rowIndex++;
    if (rowIndex >= this.searchCapacityList.length) {
      rowIndex = 0;
    }
    this.rowIndex = rowIndex;
  };

  this.incrementColIndex = function(){
    var colIndex = this.colIndex;
    colIndex++;
    if (colIndex >= this.dateList.length) {
      colIndex = 0;
    }
    this.colIndex = colIndex;
  };

  this.resetData = function(capacityList){
    var month = this.month;
    if (!moment().isSame(month,'day')) {
      fn.setDateList();
    }
    fn.setCapacityList(capacityList);
  };

  fn.initData(capacityList, month);
};
